package main.java.FSMDE.persistence;

import java.util.UUID;

import main.java.ERMDE.infrastructure.StringExtensions;
import main.java.FSMDE.models.Component;
import main.java.FSMDE.models.FactoryTransition;
import main.java.FSMDE.models.Interface;
import main.java.FSMDE.models.InterfaceCollection;
import main.java.FSMDE.models.Transition;
import main.java.FSMDE.models.TransitionControl;
import main.java.FSMDE.models.TransitionFunctional;
import main.java.FSMDE.models.TransitionType;
import main.java.common.XmlExtensions;

import org.w3c.dom.Document;
import org.w3c.dom.Element;


public class TransitionXmlManager {

	private String emptyXMLValue = "";

	public Element getXMLElementFromItem(Transition transition,
			Document document) {
		Element transitionElement = document.createElement("transition");
		transitionElement.setAttribute("id", transition.getId().toString());

		transitionElement.setAttribute("name", StringExtensions
				.isNullOrEmpty(transition.getName()) ? emptyXMLValue
				: transition.getName());

		transitionElement.setAttribute("type", transition.getType().toString());

		String sourceXML = emptyXMLValue;
		if (transition.getSource() != null)
			sourceXML = transition.getSource().getId().toString();
		transitionElement.setAttribute("source", sourceXML);

		String destinationXML = emptyXMLValue;
		if (transition.getDestination() != null)
			destinationXML = transition.getDestination().getId().toString();
		transitionElement.setAttribute("destination", destinationXML);

		if (transition.getType().equals(TransitionType.Functional)) {
			transitionElement.setAttribute("function",
					((TransitionFunctional) transition).getFunctionName());
		} else {
			Component componentTrigger = ((TransitionControl) transition)
					.getTriggerComponent();
			String idString = "";
			if (componentTrigger != null) {
				idString = componentTrigger.getId().toString();
			}
			transitionElement.setAttribute("comptrigger", idString);
		}
		return transitionElement;
	}

	public Transition getItemFromXmlElement(Element transitionElement,
			InterfaceCollection infaceColl) throws Exception {
		UUID id = UUID.fromString(transitionElement.getAttribute("id"));
		String name = XmlExtensions.getStringOrNull(transitionElement, "name");
		String attristype = transitionElement.getAttribute("type");

		String sourceXML = transitionElement.getAttribute("source");
		UUID sourceReference = null;
		Interface source = null;
		if (!sourceXML.equals(emptyXMLValue)) {
			sourceReference = UUID.fromString(sourceXML);
			source = infaceColl.get(sourceReference);
		}

		String destXML = transitionElement.getAttribute("destination");
		UUID destReference = null;
		Interface destination = null;
		if (!destXML.equals(emptyXMLValue)) {
			destReference = UUID.fromString(destXML);
			destination = infaceColl.get(destReference);
		}

		Transition transition = FactoryTransition.create(name,
				TransitionType.valueOf(attristype));
		if (source != null) {
			transition.setSource(source);
			source.getTransitions().add(transition);
		}
		if (destination != null) {
			transition.setDestination(destination);
			destination.getTransitions().add(transition);
		}
		transition.setId(id);

		if (transition.getType() == TransitionType.Control) {
			String componentTriggerId = transitionElement
					.getAttribute("comptrigger");
			if (!componentTriggerId.equals(emptyXMLValue)) {
				((TransitionControl) transition).setTriggerComponent(transition
						.getSource().getComponent(componentTriggerId));
			}
		} else {
			((TransitionFunctional) transition)
					.setFunctionName(transitionElement.getAttribute("function"));
		}
		return transition;
	}

}
